package com.bj58.ecdata.calc.dbservice.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.bj58.ecdata.calc.dbservice.consts.Constant;
import com.bj58.ecdata.calc.dbservice.dao.TrackDao_TMP;
import com.bj58.ecdata.calc.dbservice.dao.model.DataPoint;
import com.bj58.ecdata.calc.dbservice.dao.model.TrackModel;
import com.bj58.ecdata.calc.dbservice.dao.model.TrackModel_TMP;
import com.bj58.ecdata.calc.dbservice.load.SpringUtils;
import com.bj58.ecdata.calc.dbservice.service.BusslineEnum;
import com.bj58.ecdata.calc.dbservice.service.PlatformEnum;
import com.bj58.ecdata.calc.dbservice.service.TrackService_TMP;
import com.bj58.ecdata.exception.BusinessException;
import com.bj58.ecdata.util.DateUtils;
import com.bj58.ecdata.util.JsonUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

//@ServiceBehavior
public class TrackServiceImpl_TMP  implements TrackService_TMP{

	private List<String> types;
	private List<String> divTypes = Lists.newArrayList("cash", "cost");

	
	@Override
	public String ping() {
		return "PONG";
	}


	protected void checkType(String type) {
		if (!types.contains(type)) {
			throw new BusinessException("error type! (%s)", type);
		}
	}

	protected boolean isDiv(String type) {
		return divTypes.contains(type);
	}
	
	public TrackServiceImpl_TMP() {
		types = Lists.newArrayList("pv", "listpv", "vppv", "uv");
	}


	private TrackDao_TMP dao = SpringUtils.getInstance().getBean(TrackDao_TMP.class);

	@Override
	public String getDailySum(PlatformEnum platform, BusslineEnum bussline,String tag) {
		TrackModel_TMP model = new TrackModel_TMP();
		model.setTime(DateUtils.getTruncatedToday());
		model.setPlatform(platform != null ? platform.value() : null);
		model.setBusiLine(bussline != null ? bussline.value() : null);
		model.setMachineRoom(StringUtils.isNotBlank(tag) ? tag : null);

		long pv = dao.loadDailySum("pv", model);
		long vppv = dao.loadDailySum("vppv", model);

		TrackModel data = new TrackModel();
		data.setPv(pv);
		data.setVppv(vppv);
		data.setTime(dao.getLatest());
		return JsonUtils.toJson(data);
	}

	@Override
	public String getData(String type, PlatformEnum platform, BusslineEnum bussline,String tag, long time) {

		checkType(type);

		long endTime = dao.getLatest();
		List<TrackModel_TMP> params = createModels(time, endTime, Constant._2s, Constant._1h, platform, bussline,tag);
		List<DataPoint> datas = dao.load(type, params, false);
		Map<String, Object> result = Maps.newHashMap();
		result.put("data", datas);
		return JsonUtils.toJson(result);
	}

	@Override
	public String getDataTodayMinute(String type,String tag, long time) {

		checkType(type);

		long zeroTime = DateUtils.getTruncatedToday();
		long curTime = dao.getLatest();
		long endTime = DateUtils.truncateToMinute(curTime) - Constant._1m;
		List<TrackModel_TMP> params = createModels(time, endTime, Constant._1m, endTime - zeroTime + Constant._1m, null, null,null);
		List<DataPoint> datas = dao.loadMinute(type, params, false);
		Map<String, Object> result = Maps.newHashMap();
		result.put("data", datas);
		return JsonUtils.toJson(result);

	}


	@Override
	public String getYesTodayMinute(String type, String tag) {

		checkType(type);

		long zeroTime = DateUtils.getTruncatedToday();
		List<TrackModel_TMP> params = createModels(0, zeroTime, Constant._1m, Constant._1d, null, null,null);
		List<DataPoint> datas = dao.loadMinute(type, params, false);
		Map<String, Object> result = Maps.newHashMap();
		result.put("data", datas);
		return JsonUtils.toJson(result);

	}
	
	@Override
	public String getDataTodayHourly(String type, PlatformEnum platform, BusslineEnum bussline, String tag) {

		checkType(type);

		long zeroTime = DateUtils.getTruncatedToday();
		long curTime = dao.getLatest();
		long endTime = DateUtils.truncateToHour(curTime) - Constant._1h;
		List<TrackModel_TMP> params = createModels(0, endTime, Constant._1h, endTime - zeroTime + Constant._1h, platform, bussline, tag);
		List<DataPoint> datas = dao.loadHourly(type, params, false);
		Map<String, Object> result = Maps.newHashMap();
		result.put("data", datas);
		return JsonUtils.toJson(result);
	}

	@Override
	public String getDataYesterdayHourly(String type, PlatformEnum platform, BusslineEnum bussline, String tag) {

		checkType(type);

		long endTime = DateUtils.getTruncatedToday() - Constant._1h;
		List<TrackModel_TMP> params = createModels(0, endTime, Constant._1h, Constant._1d, platform, bussline, tag);
		List<DataPoint> datas = dao.loadHourly(type, params, false);
		Map<String, Object> result = Maps.newHashMap();
		result.put("data", datas);
		return JsonUtils.toJson(result);
	}

	private List<TrackModel_TMP> createModels(long startTime, long endTime, long interval, long span, PlatformEnum platform, BusslineEnum bussline,String tag) {
		long minTime = endTime - span;
		if (startTime == 0 || startTime < minTime) {
			startTime = minTime;
		}

		List<TrackModel_TMP> models = Lists.newArrayList();
		for (long i = startTime + interval; i <= endTime; i += interval) {

			TrackModel_TMP model = new TrackModel_TMP();
			model.setTime(i);
			model.setPlatform(platform != null ? platform.value() : null);
			model.setBusiLine(bussline != null ? bussline.value() : null);
			model.setMachineRoom(StringUtils.isNotBlank(tag) ? tag : null);
			models.add(model);
		}
		return models;
	}


	@Override
	public String getDailySum() {
		// TODO Auto-generated method stub
		return null;
	}

}
